package com.jiangjiang.eshop.promotion.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.promotion.constant.PromotionActivityStatus;
import com.jiangjiang.eshop.promotion.convert.PromotionActivityConvert;
import com.jiangjiang.eshop.promotion.dao.PromotionActivityMapper;
import com.jiangjiang.eshop.promotion.domain.PromotionActivityEntity;
import com.jiangjiang.eshop.promotion.domain.PromotionActivityGoodsRelationEntity;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityDTO;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityQueryPageDTO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName PromotionActivityService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/15 11:01
 * @Version 1.0
 **/
@Service
public class PromotionActivityService extends ServiceImpl<PromotionActivityMapper, PromotionActivityEntity> {
    @Autowired
    private PromotionActivityMapper promotionActivityMapper;


    /**
     * 分页查询营销活动数据
     *
     * @param dto 营销活动分页查询条件DTO
     * @return 分页数据
     */
    public Page<PromotionActivityEntity> queryForPage(PromotionActivityQueryPageDTO dto) {
        Page<PromotionActivityEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        LambdaQueryWrapper<PromotionActivityEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Strings.isNotEmpty(dto.getName()), PromotionActivityEntity::getName, dto.getName())
                .eq(dto.getStatus() != null, PromotionActivityEntity::getStatus, dto.getStatus())
                .eq(dto.getType() != null, PromotionActivityEntity::getType, dto.getType())
                .ge(Objects.nonNull(dto.getStartTime()), PromotionActivityEntity::getStartTime, dto.getStartTime())
                .le(Objects.nonNull(dto.getEndTime()), PromotionActivityEntity::getEndTime, dto.getEndTime());
        return this.page(page, wrapper);
    }

    /**
     * 查询当前需要启动的营销活动
     * @param currentDate 当前时间
     * @return 查询结果
     */
    public List<PromotionActivityEntity> queryAllNeedEnablePromotionActivity(Date currentDate) {
        LambdaQueryWrapper<PromotionActivityEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PromotionActivityEntity::getStatus, PromotionActivityStatus.DISABLED)
                .le(PromotionActivityEntity::getStartTime, currentDate);
        return this.list(wrapper);
    }


    /**
     * 查询当前需要禁用的营销活动
     * @param currentDate 当前时间
     * @return 查询结果
     */
    public List<PromotionActivityEntity> queryAllNeedDisabledPromotionActivity(Date currentDate) {
        LambdaQueryWrapper<PromotionActivityEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PromotionActivityEntity::getStatus, PromotionActivityStatus.ENABLED)
                .le(PromotionActivityEntity::getEndTime, currentDate);
        return this.list(wrapper);
    }
}
